home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2654 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.9 KB

  1. Path: crc-news.doc.ca!usenet
  2. From: Slobodan Celenkovic <slobodan@cs.unh.edu>
  3. Newsgroups: comp.lang.c++,comp.lang.pascal.delphi.misc
  4. Subject: Re: C++ with Zapp vs. Delphi
  5. Date: 18 Jan 1996 23:08:24 GMT
  6. Organization: The Communications Research Centre
  7. Message-ID: <4dmjt8$6sv@crc-news.doc.ca>
  8. References: <4coar6$d4n@sun4.bham.ac.uk> <4coip7$69s@news1.usa.pipeline.com> <DBk8wg2yqjbB083yn@iaccess.za> <4d7pmb$48c8@tigger.cc.uic.edu> <4dk38h$gdr@merlin.delphi.com> <4dksp1$3d6c@tigger.cc.uic.edu> <30fe666e.3349285@130.15.126.54>
  9. NNTP-Posting-Host: celenkovic.bob.fob003.ic.gc.ca
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15. dmurdoch@mast.queensu.ca (Duncan Murdoch) wrote:
  16. >On 18 Jan 1996 07:27:29 GMT, olczyk@sunphy1 (Jung Oh) wrote:
  17. >
  18. >>But it does raise an interesting problem.
  19. >>How do you write a class SortedList which maintains a list of objects which 
  20. >>have a method CompareTo. This lists keeps the objects in sorted order based
  21. >>on CompareTo.  
  22. >...
  23. >>If you have MI it's easy. Just create a class called SortedObject with
  24. >>a virtual method called CompareTo. Store objects of that type in the list.
  25. >
  26. >The way this is done in Delphi is to create a descendant of the
  27. >SortedList that overrides the compare method.  Instead of forcing you
  28. >to only put SortedObjects in the list, this allows you to put anything
  29. >there, even non-objects like integers or strings or empty holes, if
  30. >that suits your fancy.
  31. >
  32. >There's not a big difference, but I think this is a little bit
  33. >preferable.  It makes it clear that it's up to the list to know about
  34. >sorting, not up to the object.  That way you can easily put the same
  35. >object in multiple lists with different sorting rules.
  36. >
  37.  
  38. Nope, Jung is right. Consider (not strictly correct syntax):
  39.  
  40. Using Multiple inheritance (C++):
  41. =================================
  42.  
  43. class AbstractItem
  44. class IntItem(AbstractItem, some other class);      <- data classes
  45. class StringItem(AbstractItem, some other class);
  46. class MoneyItem(AbstractItem, some other class);
  47.  
  48. class SortedList ;
  49.  
  50. So here we have a single list and an item class for each item to be 
  51. stored in the list (plus the abstract)
  52.  
  53. AbstractItem includes:
  54. function CompareItemTo(Item2 : AbstractItem) : integer;virtual;
  55.  
  56. So each item defines one or more comparison methods like:
  57. (assume Pascal has function overloading)
  58. <MoneyItem>
  59. function CompareItemTo(Item2 : MoneyItem) : integer;virtual;
  60. function CompareItemTo(Item2 : StringItem) : integer;virtual;
  61. function CompareItemTo(Item2 : IntItem) : integer;virtual;
  62.  
  63. Consequences:
  64.  
  65. - additions/changes to the items (adding deleting an item class,...)
  66.   requires NO changes to the list. If you think about it that is exactly
  67.   what we want. Once the list is done we shouldn't have to go back
  68.   and make any changes because of items.
  69.  
  70. - Each items defines HOW it is compared to other items, and CONTROLS to
  71.   which items it may be compared. Again makes sense - the item classes
  72.   should control what they can be compared with, i.e. which comparisons
  73.   make sense.
  74.  
  75. No multiple-inheritance, no operator overloading -> Delphi:
  76. ===========================================================
  77.  
  78. We can't use the abstract item so:
  79.  
  80. class IntItem(some other class);      <- data classes
  81. class StringItem(some other class);
  82. class MoneyItem(some other class);
  83.  
  84. Instead we need a list class hierarchy with the comparison method:
  85.  
  86. class AbstractSortedList;
  87.  
  88. And then a class for each combination:
  89.  
  90. class StrStrSortedList(AbstractSortedList)
  91. function CompareItems(i1,i2 : StringItem) : integer;virtual;
  92.  
  93. class IntIntSortedList(AbstractSortedList)
  94. function CompareItems(i1,i2 : IntegerItem) : integer;virtual;
  95.  
  96.  
  97. Consequences:
  98.  
  99. - A list class for each item class, hence we end up with twice as many
  100.   classes. Furthermore we are placing some (little) item specific code   
  101.   into the list class; item specific code really belongs in the item
  102.   class!
  103.  
  104. - A list may only contain a single type of item. The MI solution's list
  105.   can store more than one type of item provided that the appropriate
  106.   comparison functions are defined (so a list contain strings, integeres
  107.   and money)
  108.  
  109.  
  110. Well in that case might as well typecast:
  111. =========================================
  112.  
  113. type ComparisonFunc : function(p1,p2:pointer) : integer;
  114.  
  115. class SortedList ;
  116.  
  117. Which would in the constructor get the appropriate comparison function as 
  118. a parameter:
  119.  
  120. Create(...., CmpFunc : ComparisonFunc);
  121.  
  122. And then list class can use it for searching, sorting,...)
  123.  
  124. Then create the function for each item:
  125.  
  126. function cmpInt(p1,p2:pointer) : integer;
  127. var i1,i2 : IntegerItem ;
  128. begin
  129.   i1 := IntegerItem(p1);
  130.   i2 := IntegerItem(p2);
  131. comparison i1 & i2
  132.  
  133. Consequences:
  134.  
  135. - Again the same number of classes as the first case. In fact very
  136.   similar, but still lists cannot combine items of different types
  137.   (actually using a more sophisticated comparison function it could also
  138.   be done). Also the item specific code is kept out of the list -> single
  139.   list class.
  140. - Not as elegant as #1.
  141. - Very dangerous. It relies on the programmer's skill to ensure that the
  142.   correct comparison function is supplied. Delphi's safe typecasting
  143.   helps. Still programmer has to now what he is doing.
  144.  
  145.  
  146. Finally to put the discussion into perspective. #1 is obviously the best 
  147. and most elegant solution. Yet even without MI (Delphi) it is doable. So 
  148. the price we pay in Delphi is that the solution is not quite as elegant.
  149.  
  150. I guess it all boils down to the size/type of project. Only the larger 
  151. projects using many classes will end up with this problem. Indeed then 
  152. C++ is more appropriate, that is when its extra firepower comes in handy. 
  153. However Delphi is designed for small/medium size apps, front ends, the 
  154. client side. There are not that many classes, they don't build some large 
  155. complex data structures,... so no need for the extras (MI, templates, 
  156. ..). Instead they kept Pascal lean, rejecting the controversial 
  157. features. So Pascal is well suited for clients (GUI), C++ for servers. 
  158. Right?
  159.  
  160.  
  161.